_Pragma("once");

#include <string>

#include "common.h"
#include "Editor.h"


#define YES_OR_NO(line, x) StringEditor(line, x, x + 5, std::array<std::string>{"yes","no"})

#define TRUE_OR_FALSE(line, x) StringEditor(line, x, x + 5, std::array<std::string>{"false","true"})



/**
 * 字符串编辑器
 *
 * @author lizb
 */
class StringListEditor:public Editor<int> {

public:
    /**
     * @param lineNum     起始行数		从1开始
     * @param startPos    起始位置（列）	从1开始
     * @param endPos      结束位置（列）
     * @param valList 编辑字符串的list
     */
    StringListEditor(int lineNum, int startPos, int endPos,const StringVectorPtr& valList) :  StringListEditor(lineNum, startPos, endPos, valList, 0) {
        if (mvalueList->empty()) {
            throw std::invalid_argument("valList 不能为空");
        }
    }

    /**
     * 初始化一个可编辑字符块，并选择初始化的下标
     * @param lineNum
     * @param startPos
     * @param endPos
     * @param valList
     * @param initIndex
     */
    StringListEditor(int lineNum, int startPos, int endPos, const StringVectorPtr& valList, int initIndex) : Editor<int>(lineNum, startPos, endPos, EditorType::STRING_LIST, 1),mvalueList(valList), mmaxIndex(mvalueList->size()) {
        value = initIndex;
    }



    bool setValueByRange(const int& val) override {
        bool result = false;
        if (this->value == val) {
            result = false;
        } else if (0 <= val && static_cast<int> (mvalueList->size()) > val) {

            Editor<int>::setValueByRange(val);

            result = true;
        } else {
            result = false;
        }

        return result;
    }



    const std::string& getText() override {
        const std::string* textValue = &mvalueList->at(value);
        if (textValue->length() > maxCharactor) {
            textBuffer.assign(*textValue, 0, maxCharactor);
            textValue = &textBuffer;
        }
        return *textValue;
    }


protected:
    const StringVectorPtr mvalueList;
    const int mmaxIndex;
};
